package org.balabala.gateway.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class SessionManager {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final long TWO_HOUR = 2*60*60;

    private static final String SESSION_KEY = "sessions";

    @SuppressWarnings("unchecked")
    public void setSession(Long userId,String session){
        redisTemplate.opsForValue().set(String.valueOf(userId),session,TWO_HOUR);
        redisTemplate.opsForHash().put(SESSION_KEY,String.valueOf(userId),System.currentTimeMillis());
    }


    public String getSession(Long userId){
        return (String)redisTemplate.opsForValue().get(userId);
    }

    @SuppressWarnings("unchecked")
    public Long deleteSession(Long userId){
        return redisTemplate.opsForHash().delete(SESSION_KEY,String.valueOf(userId));
    }
}
